home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / lib / xform.c < prev   
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.2 KB  |  161 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <math.h>
  38. #include <stdio.h>
  39. #include <GL/gl.h>
  40.  
  41. #define STACKDEPTH 10
  42.  
  43. typedef struct {
  44.     GLdouble    mat[4][4];
  45.     GLdouble    norm[3][3];
  46. } mat_t;
  47.  
  48. static mat_t matstack[STACKDEPTH] = {
  49.     {{{1.0, 0.0, 0.0, 0.0},
  50.     {0.0, 1.0, 0.0, 0.0},
  51.     {0.0, 0.0, 1.0, 0.0},
  52.     {0.0, 0.0, 0.0, 1.0}},
  53.     {{1.0, 0.0, 0.0},
  54.     {0.0, 1.0, 0.0},
  55.     {0.0, 0.0, 1.0}}}
  56. };
  57. static int identitymat = 1;
  58.  
  59. static int mattop = 0;
  60.  
  61. void m_xformpt(GLdouble pin[3], GLdouble pout[3], 
  62.     GLdouble nin[3], GLdouble nout[3])
  63. {
  64.     int    i;
  65.     GLdouble    ptemp[3], ntemp[3];
  66.     mat_t    *m = &matstack[mattop];
  67.  
  68.     if (identitymat) {
  69.     for (i = 0; i < 3; i++) {
  70.         pout[i] = pin[i];
  71.         nout[i] = nin[i];
  72.     }
  73.     return;
  74.     }
  75.     for (i = 0; i < 3; i++) {
  76.     ptemp[i] = pin[0]*m->mat[0][i] +
  77.            pin[1]*m->mat[1][i] +
  78.            pin[2]*m->mat[2][i] +
  79.            m->mat[3][i];
  80.     ntemp[i] = nin[0]*m->norm[0][i] +
  81.            nin[1]*m->norm[1][i] +
  82.            nin[2]*m->norm[2][i];
  83.     }
  84.     for (i = 0; i < 3; i++) {
  85.     pout[i] = ptemp[i];
  86.     nout[i] = ntemp[i];
  87.     }
  88.     normalize(nout);
  89. }
  90.  
  91. void m_xformptonly(GLdouble pin[3], GLdouble pout[3])
  92. {
  93.     int    i;
  94.     GLdouble    ptemp[3];
  95.     mat_t    *m = &matstack[mattop];
  96.  
  97.     if (identitymat) {
  98.     for (i = 0; i < 3; i++) {
  99.         pout[i] = pin[i];
  100.     }
  101.     return;
  102.     }
  103.      for (i = 0; i < 3; i++) {
  104.     ptemp[i] = pin[0]*m->mat[0][i] +
  105.            pin[1]*m->mat[1][i] +
  106.            pin[2]*m->mat[2][i] +
  107.            m->mat[3][i];
  108.     }
  109.     for (i = 0; i < 3; i++) {
  110.     pout[i] = ptemp[i];
  111.     }
  112. }
  113.  
  114. void m_pushmatrix(void)
  115. {
  116.     if (mattop < STACKDEPTH-1) {
  117.     matstack[mattop+1] = matstack[mattop];
  118.     mattop++;
  119.     } else
  120.     error("m_pushmatrix: stack overflow\n");
  121. }
  122.  
  123. void m_popmatrix(void)
  124. {
  125.     if (mattop > 0)
  126.     mattop--;
  127.     else
  128.     error("m_popmatrix: stack underflow\n");
  129. }
  130.  
  131. void m_translate(GLdouble x, GLdouble y, GLdouble z)
  132. {
  133.     int    i;
  134.     mat_t    *m = &matstack[mattop];
  135.  
  136.     identitymat = 0;
  137.     for (i = 0; i < 4; i++)
  138.     m->mat[3][i] = x*m->mat[0][i] +
  139.                  y*m->mat[1][i] +
  140.                  z*m->mat[2][i] +
  141.                  m->mat[3][i];
  142. }
  143.  
  144. void m_scale(GLdouble x, GLdouble y, GLdouble z)
  145. {
  146.     int    i;
  147.     mat_t    *m = &matstack[mattop];
  148.  
  149.     identitymat = 0;
  150.     for (i = 0; i < 3; i++) {
  151.     m->mat[0][i] *= x;
  152.     m->mat[1][i] *= y;
  153.     m->mat[2][i] *= z;
  154.     }
  155.     for (i = 0; i < 3; i++) {
  156.     m->norm[0][i] /= x;
  157.     m->norm[1][i] /= y;
  158.     m->norm[2][i] /= z;
  159.     }
  160. }
  161.